#!/bin/sh

#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright (C) 2005-2010 Mark Wong & Open Source Development Labs, Inc.
#

usage()
{
	echo "Usage:"
	echo "    $0 [option]"
	echo "    $0 -h"
	echo "Options:"
	echo "    -b <integer>"
	echo "        Beginning customer ordinal position"
	echo "    -c <integer>"
	echo "        Number of customers for this instance"
	echo "    -d <dbname>"
	echo "        PGDATABASE name"
	echo "    -h"
	echo "        this help"
	echo "    -i <path>"
	echo "        Path to EGen"
	echo "    -l <port>"
	echo "        database port"
	echo "    -p <database_parameters>"
	echo "        PostgreSQL database parameters"
	echo "    -r"
	echo "        Drop existing database before building a new database"
	echo "    -s <scale factor>"
	echo "        Scale factor (customers per 1 trtps)"
	echo "    -t <integer>"
	echo "        Number of customers (total)"
	echo "    -u"
	echo "        Use tablespaces"
	echo "    -w <days>"
	echo "        Initial trade days (business days) to populate" 
}

REBUILD_DB=0

# These are minimum values defined by the specification.
CUSTOMERS_INSTANCE=5000
CUSTOMERS_TOTAL=5000
SCALE_FACTOR=500
STARTING_CUSTOMER_ID=1
ITD=300

while getopts "b:c:d:hi:l:p:rs:t:uw:" OPT; do
	case ${OPT} in
	b)
		STARTING_CUSTOMER_ID=${OPTARG}
		;;
	c)
		CUSTOMERS_INSTANCE=${OPTARG}
		;;
	d)
		DBT5DBNAME=${OPTARG}
		;;
	i)
		EGENDIR=${OPTARG}
		;;
	h)
		usage
		exit 0
		;;
	l)
		PORT=${OPTARG}
		;;
	p)
		PARAMETERS=${OPTARG}
		;;
	r)
		REBUILD_DB=1
		;;
	s)
		SCALE_FACTOR=${OPTARG}
		;;
	t)
		CUSTOMERS_TOTAL=${OPTARG}
		;;
	u)
		TABLESPACES_FLAG="-t"
		;;
	w)
		ITD=${OPTARG}
		;;
	esac
done

if [ $# -eq 0 ]; then
	usage
	exit 1
fi

if [ "x${EGENDIR}" = "x" ]; then
	echo "EGENDIR not defined, specify EGen's directory."
	exit 1
else
	EGENLOADER="${EGENDIR}/bin/EGenLoader"
	if [ ! -f ${EGENLOADER} ]; then
		echo "Cannot verify that '${EGENLOADER}' exists..."
		exit 1
	fi
fi

if [ ! "x${DBT5DBNAME}" = "x" ]; then
	DBNAMEARG="-d ${DBT5DBNAME}"
	export PGDATABASE="${DBT5DBNAME}"
fi

if [ ! "x${PORT}" = "x" ]; then
	PORTARG="-p ${PORT}"
	export PGPORT="${PORT}"
fi

if [ ${REBUILD_DB} -eq 1 ]; then
	echo "Restarting the database to reset database parameters..."
	dbt5-pgsql-stop-db
	dbt5-pgsql-start-db -p "${PARAMETERS}" || exit 1
	dbt5-pgsql-drop-db ${PORTARG}
fi

#dbt5-pgsql-create-db ${PORTARG} || exit 1
psql -p ${PORT} -e -d postgres -c "DROP DATABASE ${DBT5DBNAME}" || exit 1
psql -p ${PORT} -e -d postgres -c "CREATE DATABASE ${DBT5DBNAME} ENCODING = 'SQL_ASCII' DBCOMPATIBILITY = 'PG'" || exit 1
#
# Stop and restart the database so we get the parameters we want when we start
# loading the database.
#
#dbt5-pgsql-stop-db
#dbt5-pgsql-start-db -p "${PARAMETERS}" || exit 1

# FIXME: Find a way to pass arguments to psql using EGen's -p flag, which
# apparently doesn't like arguments to have dashes (-) in them.
dbt5-pgsql-create-tables ${PORTARG} ${TABLESPACES_FLAG} || exit 1
(cd ${EGENDIR} && \
		${EGENLOADER} -b ${STARTING_CUSTOMER_ID} -l CUSTOM -f ${SCALE_FACTOR} \
		-w ${ITD} -c ${CUSTOMERS_INSTANCE} -t ${CUSTOMERS_TOTAL}) || exit 1
dbt5-pgsql-create-indexes ${PORTARG} ${TABLESPACES_FLAG} || exit 1
dbt5-pgsql-load-stored-procs ${PORTARG} || exit 1
psql ${PORTARG} -e ${DBNAMEARG} -c "SELECT setseed(0);" || exit 1
#
# VACUUM FULL ANALYZE: Build optimizer statistics for newly-created
# tables. The VACUUM FULL is probably unnecessary; we want to scan the
# heap and update the commit-hint bits on each new tuple, but a regular
# VACUUM ought to suffice for that.
#
vacuumdb ${PORTARG} -z -f ${DBNAMEARG} || exit 1
